package com.margo.project.zookeeper.action;

import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.margo.project.zookeeper.model.MargoBuildPlanVO;
import com.margo.project.zookeeper.service.MargoZookeeperService;

@RequestMapping("/zookeeper")
@Controller
public class MargoZookeeperController {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MargoZookeeperService margoZookeeperService;
	
	/**
	 * 	澶氭潯鎻掑叆鎴愬姛
	 * http://localhost:8088/zookeeper/insert
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public int insert() {
		logger.info("寮�濮嬭姹�");
		CyclicBarrier cyclicBarrier=new CyclicBarrier(8);
		for(int i=0;i<8;i++) {
			Thread thread=new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						logger.info(Thread.currentThread().getName()+"鎵ц浠诲姟");
						cyclicBarrier.await();
					} catch (InterruptedException | BrokenBarrierException e) {
						logger.error(e.getMessage(),e);
					}
					MargoBuildPlanVO margoBuildPlanVO=new MargoBuildPlanVO();
					margoBuildPlanVO.setBuildPlanId(RandomUtils.nextInt(0, 1000));
					margoBuildPlanVO.setProjectId(12);
					margoBuildPlanVO.setPlanStartDate(new Date());
					int result=margoZookeeperService.insertBuildPlan(margoBuildPlanVO);
					if(result==1) {
						logger.info(Thread.currentThread().getName()+"鎻掑叆鎴愬姛");
					}else {
						logger.info(Thread.currentThread().getName()+"鎻掑叆缁撴灉"+":"+result);
					}
				}
			});
			thread.start();
		}
		logger.info("鏌ヨ缁撴潫");
		return 1;
	}
	
	/**
	 * 	鍔犻攣浜嗕互鍚庡彧鎴愬姛涓�鏉�
	 * http://localhost:8088/zookeeper/lock/insert
	 * @return
	 */
	@RequestMapping("/lock/insert")
	@ResponseBody
	public int insertOwnedArea() {
		//String ownedArea=String.valueOf(RandomUtils.nextInt(0,12));
		String ownedArea="5";
		logger.info("寮�濮嬭姹�");
		final CyclicBarrier cyclicBarrier=new CyclicBarrier(8);
		for(int i=0;i<8;i++) {
			Thread thread=new Thread(new Runnable() {
				public void run() {
					try {
						logger.info(Thread.currentThread().getName()+"鎵ц浠诲姟");
						cyclicBarrier.await();
					} catch (InterruptedException | BrokenBarrierException e) {
						logger.error(e.getMessage(),e);
					}
					MargoBuildPlanVO margoBuildPlanVO=new MargoBuildPlanVO();
					margoBuildPlanVO.setBuildPlanId(RandomUtils.nextInt(0,1000));
					margoBuildPlanVO.setProjectId(12);
					margoBuildPlanVO.setPlanStartDate(new Date());
					int result=margoZookeeperService.insertBuildPlan(margoBuildPlanVO,ownedArea);
					if(result==1) {
						logger.info(Thread.currentThread().getName()+"鎻掑叆鎴愬姛");
					}else {
						logger.info(Thread.currentThread().getName()+"鎻掑叆缁撴灉"+":"+result);
					}
				}
			});
			thread.start();
		}
		logger.info("鏌ヨ缁撴潫");
		return 1;
	}
}
